package cn.nxl.manager.dao;

import cn.nxl.manager.vo.*;

import java.util.Map;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;

/**
 * Created by DaoCreator (daoJdbc.jt)
 */
public class ManagerJdbcDao implements ManagerDao{
    private static Logger logger = Logger.getLogger(ManagerJdbcDao.class);
    private Dao dao;

    public ManagerJdbcDao(Dao dao){
        logger.debug("Dao :"+this.getClass().getName());
        this.dao = dao;
    }

    //#appendbegin
    //==============nxl_dir==============
    String nxldir_QUERY_SQL = "select * from nxl_dir t ";

    private String createnxldirFilter(Map<String, Object> param) {
        //
        String filter = " where 1=1 ";
        if (param.get("sn") != null && !param.get("sn").equals("")) {
            filter += " and t.sn = @sn ";
        }
        if (param.get("name") != null && !param.get("name").equals("")) {
            filter += " and t.name = @name ";
        }
        if (param.get("parentDir") != null && !param.get("parentDir").equals("")) {
            filter += " and t.parentDir = @parentDir ";
        }
        if (param.get("dirLevel") != null && !param.get("dirLevel").equals("")) {
            filter += " and t.dirLevel = @dirLevel ";
        }
        if (param.get("dispOrder") != null && !param.get("dispOrder").equals("")) {
            filter += " and t.dispOrder = @dispOrder ";
        }

        return filter;
    }

    private String createnxldirOrderBy(String orderBy) {
        if (orderBy != null && !orderBy.equals(""))
            return " order by " + orderBy;
        else return "";
    }

    public void insertnxldir(nxldir vo) {
        dao.insert(vo);
    }

    public void updatenxldir(nxldir vo) {
        dao.update(vo);
    }

    public void deletenxldir(nxldir vo) {
        dao.delete(vo);
    }

    public void deletenxldirByPrimaryKey(String sn ) {
        dao.delete(nxldir.class, sn);
    }

    public void deletenxldirByParam(Object... param) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        for (int i = 0 ; i<param.length; i += 2) {
            map.put(param[i].toString(), param[i + 1]);
        }

        deletenxldirByParam(map);
    }

    public void deletenxldirByParam(Map<String, Object> param) {
        String filter = createnxldirFilter(param);
        //
        if (!filter.trim().toLowerCase().equals("where 1=1")) {
            String sql = "delete from nxl_dir t " + filter;
            Sql s = Sqls.create(sql);
            s.params().putAll(param);
            dao.execute(s);
        } 
    }

    public nxldir getnxldirByPrimaryKey(String sn ) {
        String sql= nxldir_QUERY_SQL + " where t.sn = @sn  ";
        Sql s = Sqls.create(sql);
        //todo:
        s.params().set("sn", sn);
        s.setCallback(Sqls.callback.entities());
        s.setEntity(dao.getEntity(nxldir.class));
        dao.execute(s);
        return s.getObject(nxldir.class);
    }

    public int getnxldirCountByPrimaryKey(String sn ) {
        String sql="select count(sn) from nxl_dir t where t.sn = @sn   ";
        Sql s = Sqls.create(sql);
        //todo:
        s.params().set("sn", sn);
        s.setCallback(Sqls.callback.integer());
        dao.execute(s);
        return s.getInt();
    }

    public int getnxldirCountByParam(Object... param) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        for (int i = 0 ; i<param.length; i += 2) {
            map.put(param[i].toString(), param[i + 1]);
        }

        return getnxldirCountByParam(map);
    }

    public int getnxldirCountByParam(Map<String, Object> param) {
        //�����Ӳ�ѯ���� ��Ҫ�޸� createnxldirFilter
        String filter = createnxldirFilter(param);
        String sql = "select count(sn) from nxl_dir t " + filter;
        Sql s = Sqls.create(sql);
        s.params().putAll(param);
        s.setCallback(Sqls.callback.integer());
        dao.execute(s);
        return s.getInt();
    }

    public List<nxldir> getnxldirListByParam(String orderBy, Object... param) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        for (int i = 0 ; i < param.length ; i += 2) {
            map.put(param[i].toString(), param[i + 1]);
        }

        return getnxldirListByParam(orderBy, map);
    }

    public List<nxldir> getnxldirListByParam(int page, int count, String orderBy, Object... param) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        for (int i = 0 ; i < param.length ; i += 2) {
            map.put(param[i].toString(), param[i + 1]);
        }

        return getnxldirListByParam(orderBy, map, page, count);
    }

    public List<nxldir> getnxldirListByParam(String orderBy, Map<String, Object> param) {
        //�����Ӳ�ѯ���� ��Ҫ�޸� createnxldirFilter
        String filter = createnxldirFilter(param);
        String sql = "select * from (" + nxldir_QUERY_SQL + filter + ") dataall " + createnxldirOrderBy(orderBy);
        Sql s = Sqls.create(sql);
        s.params().putAll(param);
        s.setCallback(Sqls.callback.entities());
        s.setEntity(dao.getEntity(nxldir.class));
        dao.execute(s);
        return s.getList(nxldir.class);
    }

    public List<nxldir> getnxldirListByParam(String orderBy, Map<String, Object> param, int page, int count) {
        param.put("row0", (page - 1) * count);
        param.put("row1", count);

        //�����Ӳ�ѯ���� ��Ҫ�޸� createnxldirFilter
        String filter = createnxldirFilter(param);
        String sql =  "select * from (" + nxldir_QUERY_SQL + filter + ") dataall " + createnxldirOrderBy(orderBy) + " limit @row0, @row1";

        Sql s = Sqls.create(sql);
        s.params().putAll(param);
        s.setCallback(Sqls.callback.entities());
        s.setEntity(dao.getEntity(nxldir.class));
        dao.execute(s);
        return s.getList(nxldir.class);


    }

    public List<nxldir> getAllnxldirList(String orderBy){
	    String sql = nxldir_QUERY_SQL;
		Sql s = Sqls.create("select * from (" + sql + ") dataall " + createnxldirOrderBy(orderBy));
        s.setCallback(Sqls.callback.entities());
        s.setEntity(dao.getEntity(nxldir.class));
        dao.execute(s);
        return s.getList(nxldir.class);
    }
    //==============end of nxl_dir==============
    //#appendend

    //==============nxl_content==============
    String nxlcontent_QUERY_SQL = "select * from nxl_content t ";

    private String createnxlcontentFilter(Map<String, Object> param) {
        //
        String filter = " where 1=1 ";
        if (param.get("sn") != null && !param.get("sn").equals("")) {
            filter += " and t.sn = @sn ";
        }
        if (param.get("cType") != null && !param.get("cType").equals("")) {
            filter += " and t.cType = @cType ";
        }
        if (param.get("dirSn") != null && !param.get("dirSn").equals("")) {
            filter += " and t.dirSn = @dirSn ";
        }
        if (param.get("content") != null && !param.get("content").equals("")) {
            filter += " and t.content = @content ";
        }
        if (param.get("dispOrder") != null && !param.get("dispOrder").equals("")) {
            filter += " and t.dispOrder = @dispOrder ";
        }

        return filter;
    }

    private String createnxlcontentOrderBy(String orderBy) {
        if (orderBy != null && !orderBy.equals(""))
            return " order by " + orderBy;
        else return "";
    }

    public void insertnxlcontent(nxlcontent vo) {
        dao.insert(vo);
    }

    public void updatenxlcontent(nxlcontent vo) {
        dao.update(vo);
    }

    public void deletenxlcontent(nxlcontent vo) {
        dao.delete(vo);
    }

    public void deletenxlcontentByPrimaryKey(String sn ) {
        dao.delete(nxlcontent.class, sn);
    }

    public void deletenxlcontentByParam(Object... param) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        for (int i = 0 ; i<param.length; i += 2) {
            map.put(param[i].toString(), param[i + 1]);
        }

        deletenxlcontentByParam(map);
    }

    public void deletenxlcontentByParam(Map<String, Object> param) {
        String filter = createnxlcontentFilter(param);
        //
        if (!filter.trim().toLowerCase().equals("where 1=1")) {
            String sql = "delete from nxl_content t " + filter;
            Sql s = Sqls.create(sql);
            s.params().putAll(param);
            dao.execute(s);
        } 
    }

    public nxlcontent getnxlcontentByPrimaryKey(String sn ) {
        String sql= nxlcontent_QUERY_SQL + " where t.sn = @sn  ";
        Sql s = Sqls.create(sql);
        //todo:
        s.params().set("sn", sn);
        s.setCallback(Sqls.callback.entities());
        s.setEntity(dao.getEntity(nxlcontent.class));
        dao.execute(s);
        return s.getObject(nxlcontent.class);
    }

    public int getnxlcontentCountByPrimaryKey(String sn ) {
        String sql="select count(sn) from nxl_content t where t.sn = @sn   ";
        Sql s = Sqls.create(sql);
        //todo:
        s.params().set("sn", sn);
        s.setCallback(Sqls.callback.integer());
        dao.execute(s);
        return s.getInt();
    }

    public int getnxlcontentCountByParam(Object... param) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        for (int i = 0 ; i<param.length; i += 2) {
            map.put(param[i].toString(), param[i + 1]);
        }

        return getnxlcontentCountByParam(map);
    }

    public int getnxlcontentCountByParam(Map<String, Object> param) {
        //�����Ӳ�ѯ���� ��Ҫ�޸� createnxlcontentFilter
        String filter = createnxlcontentFilter(param);
        String sql = "select count(sn) from nxl_content t " + filter;
        Sql s = Sqls.create(sql);
        s.params().putAll(param);
        s.setCallback(Sqls.callback.integer());
        dao.execute(s);
        return s.getInt();
    }

    public List<nxlcontent> getnxlcontentListByParam(String orderBy, Object... param) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        for (int i = 0 ; i < param.length ; i += 2) {
            map.put(param[i].toString(), param[i + 1]);
        }

        return getnxlcontentListByParam(orderBy, map);
    }

    public List<nxlcontent> getnxlcontentListByParam(int page, int count, String orderBy, Object... param) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        for (int i = 0 ; i < param.length ; i += 2) {
            map.put(param[i].toString(), param[i + 1]);
        }

        return getnxlcontentListByParam(orderBy, map, page, count);
    }

    public List<nxlcontent> getnxlcontentListByParam(String orderBy, Map<String, Object> param) {
        //�����Ӳ�ѯ���� ��Ҫ�޸� createnxlcontentFilter
        String filter = createnxlcontentFilter(param);
        String sql = "select * from (" + nxlcontent_QUERY_SQL + filter + ") dataall " + createnxlcontentOrderBy(orderBy);
        Sql s = Sqls.create(sql);
        s.params().putAll(param);
        s.setCallback(Sqls.callback.entities());
        s.setEntity(dao.getEntity(nxlcontent.class));
        dao.execute(s);
        return s.getList(nxlcontent.class);
    }

    public List<nxlcontent> getnxlcontentListByParam(String orderBy, Map<String, Object> param, int page, int count) {
        param.put("row0", (page - 1) * count);
        param.put("row1", count);

        //�����Ӳ�ѯ���� ��Ҫ�޸� createnxlcontentFilter
        String filter = createnxlcontentFilter(param);
        String sql =  "select * from (" + nxlcontent_QUERY_SQL + filter + ") dataall " + createnxlcontentOrderBy(orderBy) + " limit @row0, @row1";

        Sql s = Sqls.create(sql);
        s.params().putAll(param);
        s.setCallback(Sqls.callback.entities());
        s.setEntity(dao.getEntity(nxlcontent.class));
        dao.execute(s);
        return s.getList(nxlcontent.class);


    }

    public List<nxlcontent> getAllnxlcontentList(String orderBy){
	    String sql = nxlcontent_QUERY_SQL;
		Sql s = Sqls.create("select * from (" + sql + ") dataall " + createnxlcontentOrderBy(orderBy));
        s.setCallback(Sqls.callback.entities());
        s.setEntity(dao.getEntity(nxlcontent.class));
        dao.execute(s);
        return s.getList(nxlcontent.class);
    }

    @Override
    public List<Map> getDirSub(String dirSn, int page, int count) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("row0", (page - 1) * count);
        param.put("row1", count);
        param.put("dirSn", dirSn);
        String sql = "select sn, name, 'dir' as ctype, DISPORDER as dorder, ctype as contenttype, content  " +
                "from nxl_dir where PARENTDIR = @dirSn \n" +
                "union \n" +
                "select sn, title as name, 'content' as ctype, DISPORDER as dorder, ctype as contenttype, content " +
                "from nxl_content where DIRSN = @dirSn\n" +
                "order by dorder";
        sql = "select * from (" + sql + ") dataall limit @row0, @row1 ";
        Sql s = Sqls.create(sql);
        s.setCallback(Sqls.callback.records());
        //s.setCallback(Sqls.callback.entities());
        //s.setEntity(dao.getEntity(HashMap.class));
        s.params().putAll(param);
        dao.execute(s);
        return s.getList(Map.class);
    }

    @Override
    public int getDirSubCount(String dirSn) {
        String sql = "select sum(snc) from (\n" +
                "select count(sn) as snc from nxl_content where DIRSN=@dirSn\n" +
                "UNION all\n" +
                "select count(sn) as snc from nxl_dir where PARENTDIR=@dirSn\n" +
                ") t";
        Sql s = Sqls.create(sql);
        s.setCallback(Sqls.callback.integer());
        s.params().set("dirSn", dirSn);
        dao.execute(s);
        return s.getInt();
    }
    //==============end of nxl_content==============
    

}
